package com.elinkway.infinitemovies.view;

import android.app.Activity;
import android.view.View;
import android.widget.RelativeLayout;

import com.baidu.mobads.AdSize;
import com.baidu.mobads.InterstitialAd;
import com.baidu.mobads.InterstitialAdListener;
import com.elinkway.infinitemovies.listener.MyOnAdListener;
import com.elinkway.infinitemovies.selfdata.MyDataRecordCreater;
import com.elinkway.infinitemovies.selfdata.SelfDataConstant;
import com.elinkway.infinitemovies.selfdata.bean.MyDataRecord;
import com.elinkway.infinitemovies.selfdata.dao.RealmDaoManager;
import com.elinkway.infinitemovies.utils.AdStatisticsSp;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.le123.ysdq.R;

import java.util.HashMap;

/**
 * Author: liuxiaojiang(xiao)
 * Date: 2017-04-18
 * Time: 15:50
 * Description: FIXME
 */
public class BaiduInterAdView {
    private Activity activity;
    private View mView;
    private long startRequestTime = 0;
    private String mAdposition;
    private InterstitialAd mBaiduInterAd;
    private MyOnAdListener adListener;

    public MyOnAdListener getAdListener() {
        return adListener;
    }

    public void setAdListener(MyOnAdListener adListener) {
        this.adListener = adListener;
    }

    public BaiduInterAdView(Activity activity, View view, String mAdposition){
        this.activity = activity;
        this.mView = view;
        this.mAdposition = mAdposition;
    }

    public BaiduInterAdView(Activity activity, String mAdposition){
        this.activity = activity;
        this.mAdposition = mAdposition;
    }

    public void setBaiduAd(String adId){
        mBaiduInterAd = new InterstitialAd(activity, AdSize.InterstitialForVideoPausePlay, adId);
        mView.findViewById(R.id.ad_logo_iv).setVisibility(View.GONE);
        mView.findViewById(R.id.pause_ad_image).setVisibility(View.GONE);
        mView.findViewById(R.id.ad_close_iv).setVisibility(View.GONE);
        mView.findViewById(R.id.ad_tips_tv).setVisibility(View.GONE);
        mView.findViewById(R.id.baidu_inter_ad_rl).setVisibility(View.VISIBLE);
        startRequestTime = System.currentTimeMillis();
        mBaiduInterAd.setListener(new InterstitialAdListener() {

            @Override
            public void onAdClick(InterstitialAd arg0) {
                reportAdClick();
            }

            @Override
            public void onAdDismissed() {
                mView.setVisibility(View.GONE);
            }

            @Override
            public void onAdFailed(String arg0) {
                reportAdRequestError();
            }

            @Override
            public void onAdPresent() {
            }

            @Override
            public void onAdReady() {
                if(adListener != null){
                    adListener.onAdSuccess(null);
                }
                reportAdSuccess();
                if (mBaiduInterAd.isAdReady()) {
                    reportAdShow();
                    mBaiduInterAd.showAdInParentForVideoApp(activity, (RelativeLayout) mView.findViewById(R.id.baidu_inter_ad_rl));
                }
            }
        });
        mBaiduInterAd.loadAdForVideoApp(UIs.dipToPx(240), UIs.dipToPx(160));
        reportAdRequestVtwo();
    }

    public void setExitBaiduAd(String adId){
        mBaiduInterAd = new InterstitialAd(activity, AdSize.InterstitialForVideoPausePlay, adId);
        startRequestTime = System.currentTimeMillis();
        mBaiduInterAd.setListener(new InterstitialAdListener() {

            @Override
            public void onAdClick(InterstitialAd arg0) {
                reportAdClick();
            }

            @Override
            public void onAdDismissed() {
                adListener.onAdDismissed();
            }

            @Override
            public void onAdFailed(String arg0) {
                reportAdRequestError();
                AdStatisticsSp.savaOneUp(activity, AdStatisticsSp.BAIDUEXITADERROR);
            }

            @Override
            public void onAdPresent() {
            }

            @Override
            public void onAdReady() {
                reportAdSuccess();
                AdStatisticsSp.savaOneUp(activity, AdStatisticsSp.BAIDUEXITADSUCCESS);
                if (mBaiduInterAd.isAdReady()) {
                    reportAdShow();
                    AdStatisticsSp.savaOneUp(activity, AdStatisticsSp.BAIDUEXITADSHOW);
                    mBaiduInterAd.showAdInParentForVideoApp(activity, (RelativeLayout) mView);
                }
            }
        });
        // 广告请求的尺寸以及展现时传入的父控件尺寸都要合理，不能过小。
        mBaiduInterAd.loadAdForVideoApp(UIs.dipToPx(320), UIs.dipToPx(267));
        AdStatisticsSp.savaOneUp(activity, AdStatisticsSp.BAIDUEXITADREQUEST);
        reportAdRequestVtwo();
    }

    public void showExitAd(){
        if (mBaiduInterAd != null) {
            if (mBaiduInterAd.isAdReady()) {
                mBaiduInterAd.showAd(activity);
            } else {
                mBaiduInterAd.loadAdForVideoApp(UIs.dipToPx(320), UIs.dipToPx(267));
            }
        }
    }

    private void reportAdSuccess(){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        gdtPropertyMap.put("result", "success");
        gdtPropertyMap.put("time", (System.currentTimeMillis()-startRequestTime)/1000+"");
        UmengEventPoster.doPost(UmengEventPoster.BAIDUAD_REQUEST, gdtPropertyMap);

        selfBaiduReport(SelfDataConstant.AC_ADREQUEST_SUCCESS);
    }

    private void reportAdRequestError(){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        gdtPropertyMap.put("result", "fail");
        UmengEventPoster.doPost(UmengEventPoster.BAIDUAD_REQUEST, gdtPropertyMap);

        selfBaiduReport(SelfDataConstant.AC_ADREQUEST_FAIL);
    }

    private void reportAdError(int code){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        gdtPropertyMap.put("code", code+"");
        UmengEventPoster.doPost(UmengEventPoster.BAIDUAD_ERROE, gdtPropertyMap);
    }

    private void reportAdShow() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.BAIDUAD_SHOW, gdtPropertyMap);

        selfBaiduReport(SelfDataConstant.AC_ADSHOW);
    }

    private void reportAdClick() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.BAIDUAD_CLICK, gdtPropertyMap);

        selfBaiduReport(SelfDataConstant.AC_ADCLICK);
    }

    private void reportAdRequestVtwo(){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.BAIDUAD_REQUEST_VTWO, gdtPropertyMap);

        selfBaiduReport(SelfDataConstant.AC_SDKAD_REQUEST);
    }

    private void selfBaiduReport(String acode){
        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(acode);
        record.setAd_pro(SelfDataConstant.AD_BD);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }
}
